import * as ajax from '@/api/api'

export default {
  async setUserInfo({
    state,
    commit
  }, payload) {
    let res = await ajax.getUserInfor()
    if (res.status === 200 && res.data) {
      if (res.data.org.id == 391) {
        res.data.resources.unshift({
          hierarchy: 1,
          isDisplay: 1,
          isRelateOrganization: 0,
          link: "smartSpray",
          resourceId: 105999,
          resourceName: "简易喷淋",
          optAction: []
        })
      }
      commit('SET_USER_INFO', {
        info: res.data,
        isLogin: true,
        callback: payload
      })
    } else {
      commit('SET_USER_INFO', {
        info: null,
        isLogin: false,
        callback: payload
      })
    }
  },
  async requestLogin({
    state,
    commit
  }, payload) {
    let res = await ajax.requestLogin(payload)
    if (res.status === 200 && res.data.resources) { // 登录成功dispatch
      commit('GET_USER_INFO', {
        info: res.data,
        isLogin: true
      })
    } else {
      commit('GET_USER_INFO', {
        info: res,
        isLogin: false
      })
    }
  }
}
